package com.application.model.elegircolegio.retrieve;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;

import com.application.model.RetrivierWeb;
import com.application.model.elegircolegio.ManagerElegirColegioEntity;
import com.application.model.elegircolegio.entries.ElegirColegioEntity;

public class ManagerDetalle extends RetrivierWeb{

	private ManagerElegirColegioEntity informacion; 
	
	public ManagerDetalle(ManagerElegirColegioEntity informacion) {
		this.informacion = informacion;
	}

	protected void processContentDetalle(String data,String pais,String provincia,String partido,String nombreColegio,String zonaKey) {
        Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        String      nameColegio = "";
        String      infrastructura = "";
        String      descripcion = "";
        data = unescapeHtml(data);
//        data = StringEscapeUtils.escapeSql(data);
        //regex = "<h1>(.*?)</h1>(.*?)<strong>Idiomas</strong><br />(.*?).<br />(.*?)<strong>Infraestructura</strong>(.*?)<br />(.*?)<div style=\\\"text-align:justify;\\\">(.*?)<!-- ARTICULOS COMPLEMENTARIOS -->";
        regex = "<h1>(.*?)</h1>(.*?)<div style=\\\"text-align:justify;\\\">(.*?)<!-- ARTICULOS COMPLEMENTARIOS -->";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
            nameColegio = matcher.group(1);
            
            infrastructura = getInfrastructura(matcher.group(2));
            descripcion = getDescripcion(matcher.group(3));
//            if(nameColegio.equals("San Felipe Apóstol")){
//            	System.out.println("");
//            }
            
            logger.info("INSERT INTO detalle(infraestructura,descripcion,Institucion_id) "
                    + "VALUES("
                    + "'"+escapeSql(infrastructura)+"',"                    
                    + "'"+escapeSql(descripcion)+"',"
                    + " (SELECT id from institucion WHERE "+getWhereColegioUpdate(pais, provincia, partido, nameColegio,zonaKey)+")"
                    + ");");
            informacion.addInfrastructuraYDescripcion(nameColegio,infrastructura,descripcion);
            
            generarIdiomas(matcher.group(2),pais, provincia, partido, nameColegio,zonaKey);
        }
    }
	
	private String getDescripcion(String data) {
		String descripcion = "";
		descripcion = data.replaceAll("<img(.*?)>", "");
//		descripcion = data.replaceAll("'", "\'");
		if(descripcion.matches("<img(.*?)>")){
			System.out.println("");
		}
		if(descripcion.contains("<img")){
			System.out.println("");
		}
		return descripcion;
	}

	private void generarIdiomas(String data,String pais,String provincia,String partido, String nameColegio,String zonaKey) {
		Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        
        String      idiomasLinea = "";
        String[]    idiomasVector = null;
        
        regex = "<strong>Idiomas</strong><br />(.*?).<br />";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
        	
        	idiomasLinea = matcher.group(1);
        	idiomasVector = idiomasLinea.split(",");
        	if(idiomasVector!=null){
        		for (String idioma : idiomasVector) {
        			idioma = idioma.trim();
        			if(!idioma.isEmpty()){
	        			logger.info("INSERT INTO idioma_institucion(idioma_id,institucion_id) "
	                            + "VALUES("
	                            + " (SELECT id from idioma WHERE UPPER(name)=UPPER('"+idioma+"')),"
	                            + " (SELECT id from institucion WHERE "+getWhereColegioUpdate(pais, provincia, partido, nameColegio,zonaKey)+")"
	                            + ");");
	        			informacion.addIdioma(nameColegio,idioma);
        			}else{
        				System.out.println("");
        			}
				}
        	}            
        }
    }

    private String getInfrastructura(String data) {
    	Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        String      infrastructura = "";
        regex = "<strong>Infraestructura</strong>(.*?)<br />(.*?)<!-- PRESENTACION -->";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
            infrastructura = matcher.group(2);
        }
        infrastructura = infrastructura.replace("<br />", "");
        return infrastructura;
    }
	@Override
	public void processContent(String data, String parentName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void retrieve() {
		// TODO Auto-generated method stub
		
	}

	public void retrivie(String urlDetalle,String beginPatter,String endPatter,String pais,String provincia,String partido,String nombreColegio,String zonaKey) {
		String paginaWeb = "";
		try {
			paginaWeb = getWebPageContent(urlDetalle, beginPatter, endPatter,"utf-8");
			if (!paginaWeb.isEmpty()) {
//                                if(parentName!=null){
//                                    desendencia.add(parentName);
//                                }                                
				processContentDetalle(paginaWeb,pais,provincia,partido,nombreColegio,zonaKey);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		
	}

}
